package com.sura.deployment.xls;

import java.io.*;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;


public class ChangesFile {
	private static File file;
	private static FileInputStream fileInputStream;
	private static FileOutputStream fileOutputStream;
		
	public static void loadFile()throws IOException{
		file = new File("./Cambios.xls");
		message(0);
	}
	
	public static void loadFileInputStream()throws IOException{
		fileInputStream = new FileInputStream(file);
		message(1);
	}
	
	public static void loadFileOutputStream()throws IOException{
		fileOutputStream = new FileOutputStream(file);
		message(2);
	}
	
	public static void closeFileInputStream()throws IOException{
		fileInputStream.close();
		message(3);
	}
	
	public static void closeFileOutputStream()throws IOException{
		fileOutputStream.close();
		message(4);
	}
	
	public static void doValidation(String 	nameAnalyst, int initLine, int endtLine)throws IOException{
		
		POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
		HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
		HSSFSheet sheet = workBook.getSheet("SolicitudCambiosAplicacion");
		String[][]  changedElements = new String[endtLine-initLine+1][9];
		
		for(int i=0; i<=(endtLine-initLine); i++){
			for(int j=0; j<9; j++){
				HSSFCell cell = sheet.getRow(initLine+i-1).getCell(j);
				if(cell != null)
					changedElements[i][j] = cell.toString();
				else
					changedElements[i][j] = null;
			}
		}
		
		for(int i=0; i<=(endtLine-initLine); i++){
			if(changedElements[i][2] == null || changedElements[i][3] == null || changedElements[i][5] == null 
					|| changedElements[i][7] != null || changedElements[i][8] != null)
				message(6);
		}

		message(5);
		Session.setChangedElements(changedElements);
	}
	
	
	
	public static void message(int i){
		switch (i){
			case 0:
				System.out.println("OK: Archivo de cambios caragado");
				break;
			case 1:
				System.out.println("OK: Flujo de lectura abierto");
				break;
			case 2:
				System.out.println("OK: Flujo de escritura abierto");
				break;
			case 3:
				System.out.println("OK: Flujo de lectura cerrado");
				break;
			case 4:
				System.out.println("OK: Flujo de escritura cerrado");
				break;
			case 5:
				System.out.println("OK: Archivo de cambios bien formateado");
				break;
			default:
				System.err.println("ERROR: Archivo de cambios mal formateado");
				System.exit(1);
		}
			
		
		
	}
	
}
